﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，成员操作
    /// </summary>
    public class StaffAccessDAL : DalBase, IStaffAccess
    {
        public StaffAccessDAL() 
        {
            Schema = "am.";
            ViewTableName = "V_StaffAccess";
            Fields = "AccessID, IP, AccessDescription, AddTime, StaffID, Username, Email";
        }

        #region IStaffAccess 成员

        /// <summary>
        /// 新增操作记录
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool InsertStaffAccess(StaffAccessInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@StaffID", SqlDbType.Int)
                ,new SqlParameter("@IP", SqlDbType.VarChar,15)
                ,new SqlParameter("@AccessDescription",SqlDbType.NVarChar,500)
                ,new SqlParameter("@RtnCode", SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.Staff.StaffID;
            parm[i++].Value = info.IP;
            parm[i++].Value = info.AccessDescription;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddStaffAccess", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 获取登录记录列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<StaffAccessInfo> ListStaffAccess(StaffAccessCondition condition, out int recordCount)
        {
            Wheres = new StringBuilder(" 1 = 1 ");
            if (condition.StaffID > 0)
                Wheres.AppendFormat(" AND StaffID = {0}", condition.StaffID);
            recordCount = base.PageListCounter(Schema + ViewTableName, Wheres.ToString());
            SqlDataReader dr = base.PageListRecord(Fields, Schema + ViewTableName, Wheres.ToString()
                , condition.OrderBy, condition.PageIndex, condition.PageSize);
            List<StaffAccessInfo> list = new List<StaffAccessInfo>();
            if (dr != null)
            {
                //LoginID, IP, AddTime, StaffID, Username, Email
                while (dr.Read())
                {
                    StaffAccessInfo info = new StaffAccessInfo();
                    int i = 0;
                    info.AccessID = dr.GetInt32(i++);
                    info.IP = dr.GetString(i++);
                    info.AccessDescription = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.Staff.Username = dr.GetString(i++);
                    info.Staff.Email = dr.GetString(i++);
                    list.Add(info);
                }
            }
            return list;
        }

        #endregion
    }
}
